from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from apps.headline.headline_serializers import *
# from rest_framework.pagination import PageNumberPagination
from apps.utils.pages import MyPage
from rest_framework.response import Response
from rest_framework.views import APIView
# Create your views here.


class ChannelListViews(ModelViewSet):
    queryset = Channel.objects.all()
    serializer_class = ChannelSerializers

    pagination_class = MyPage


# 文章列表
class ArticleListView(ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleModelSerializers

    pagination_class = MyPage


    def get_queryset(self):
        pk = self.kwargs.get('id')
        if pk == '-1':
            return Article.objects.all()
        else:
            channel = Channel.objects.get(id=pk)

            return self.queryset.filter(channel=channel)


# 收藏

class ArticleListViewCollect(ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleModelSerializers


    def update(self, request, *args, **kwargs):

        pk = self.kwargs.get('pk')
        article = Article.objects.get(pk=pk)
        if self.request.user.collected_articles.filter(pk=pk):
            self.request.user.collected_articles.remove(article)
            return Response({'message':'取消收藏成功','success':True})
        else:
            self.request.user.collected_articles.add(article)
            return Response({'message': '收藏成功', 'success': True})


# # 文章详情
class ArticleMessage(ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleModelSerializers
#
#     def retrieve(self, request, *args, **kwargs):
#         pk = self.kwargs.get('pk')
#
#         article = Article.objects.get(pk=pk)
#         s = ArticleModelSerializers(article)
#         return Response(s.data)


# 文章详情  /article/{id}/
class ArticleDetailView(APIView):

    def get(self,request, id):
        article = Article.objects.get(id=id)
        serializer = ArticleModelSerializers(article)
        return Response(serializer.data)




# 发布文章
class IssueArticle(ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleModelSerializers

    def create(self, request, *args, **kwargs):
        user = request.user

        if not user:
            return Response({'message': '未登录', 'success': False})
        else:
            data = request.data
            data['user'] = user.id
            s = ArticleSerializerForCreate(data=data)
            s.is_valid(raise_exception=True)
            article = s.save()
            return Response({'articleid':article.id,'message':'发布成功','success':True})


# 评论文章

class ContentArticle(ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleCommentSerializers

    def create(self, request, *args, **kwargs):
        user = request.user

        if not user:
            return Response({'message': '未登录', 'success': False})
        else:
            pk = self.kwargs.get('pk')
            data = request.data
            article = Article.objects.get(pk=pk)
            data['article']=pk
            data['user'] = user.id
            s = ArticleCommentSerializers(data=data)
            s.is_valid(raise_exception=True)
            s.save()

            # Article.objects.filter(id=article.id).update(comment_count=Article['comment_count']+1)

            return Response({
                "message": "评论成功",
                'success': True
            })

# 搜索文章
class SearchView(ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleModelSerializers
    pagination_class = MyPage

    def get_queryset(self):
        text = self.request.query_params.get('text')
        if text:
            return Article.objects.filter(title__contains=text) or Article.objects.filter(content__contains=text)
        else:
            return Article.objects.all()







